﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Configuration;
using KPIS.DBM;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.DAL
{
    public class EssMessageDAL
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public EssMessageDAL()
        {

        }
        public string GetNextSendRefNo(string SendType)
        {
            string strResult = "";
            int nNextRunningNo;
            DataTable dtb;
            string strSQL;
            dbManager.Open();
            dbManager.CreateParameters(2);

            dbManager.AddParameters(0, "@RequestType", SendType);
            dbManager.AddParameters(1, "@ActiveRecord", SystemInfo._ActiveRecordStatus);

            strResult = SendType + "-" + string.Format("{0:yyyyMMdd}", DateTime.Today) + "-";
            strSQL = "SELECT MAX(REF_NO) AS MAX_REF_NO"
                + " FROM MESSAGE_SEND"
                + " WHERE REF_NO LIKE '" + strResult + "%'";
            dtb = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0];

            if (dtb.Rows.Count > 0)
            {
                string strText = dtb.Rows[0]["MAX_REF_NO"].ToString();
                strText = strText.Length > 0 ? strText : "000";
                nNextRunningNo = int.Parse(strText.Substring(strText.Length - 3, 3)) + 1;
                // nNextRunningNo = int.Parse(string strResult string.ri.Right(Misc.NullValue(drc[0][0], "0000").ToString(), 4)) + 1;
                strResult += nNextRunningNo.ToString("000");
            }

            return strResult;
        }
        public void Send(EssMessageInfo info)
        {
            string SendRefNo = GetNextSendRefNo("NT");
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(14);

                dbManager.AddParameters(0, "@send_seq", info.SendSequence);
                dbManager.AddParameters(1, "@send_date", info.SendDate);
                dbManager.AddParameters(2, "@ref_seq", info.RefSequence);
                dbManager.AddParameters(3, "@ref_type", info.RefType);
                dbManager.AddParameters(4, "@ref_no", SendRefNo);
                dbManager.AddParameters(5, "@send_text", info.SendText);
                dbManager.AddParameters(6, "@from_emp_seq", info.FromSequence);
                dbManager.AddParameters(7, "@to_emp_seq", info.ToSequence);
                dbManager.AddParameters(8, "@from_send_seq", info.FromSendSequence);
                dbManager.AddParameters(9, "@remarks", info.Remarks);
                dbManager.AddParameters(10, "@message_status", info.MessageStatus);
                dbManager.AddParameters(11, "@record_status", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(12, "@created_by", info.CreatedBy);
                dbManager.AddParameters(13, "@created_when", DateTime.Today);

                strSQL = "INSERT INTO MESSAGE_SEND"
                    + "(SEND_DATE, REF_SEQ, REF_TYPE, REF_NO, SEND_TEXT, FROM_EMP_SEQ, TO_EMP_SEQ, FROM_SEND_SEQ"
                    + ", REMARKS, MESSAGE_STATUS, RECORD_STATUS, CREATED_BY, CREATED_WHEN)"
                    + " VALUES(@send_date"
                        + ", @ref_seq"
                        + ", @ref_type"
                        + ", @ref_no"
                        + ", @send_text"
                        + ", @from_emp_seq"
                        + ", @to_emp_seq"
                        + ", @from_send_seq"
                        + ", @remarks"
                        + ", @message_status"
                        + ", @record_status"
                        + ", @created_by"
                        + ", @created_when)";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
    }
}
